package tool

import (
	"go.uber.org/zap"
	"regexp"
	m "spider/my_logger"
)

func GetLevel(code string) int {
	switch len(code) {
	case 0:
		return 1
	case 2:
		return 2
	case 4:
		return 3
	case 6:
		return 4
	case 9:
		return 5
	}
	return 0
}

func GetCode(url string) string {
	reg := regexp.MustCompile(`^.+/(\d+)\.html$`)
	if reg.Match([]byte(url)) {
		return reg.FindStringSubmatch(url)[1]
	}
	return ""
}

func PanicOnError(err error, msg ...string) {
	if err != nil {
		if len(msg) > 0 {
			m.Panic(err.Error(), zap.String("msg", msg[0]))
		} else {
			m.Panic(err.Error())
		}
	}
}
